package com.mrd.gtimpframe.action;

import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimpframe.service.PhoneNOBindingService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by dusy on 2017/7/20.
 */
@Controller
@RequestMapping("/user")
public class PhoneNOBindingController {
    @Autowired
    private PhoneNOBindingService phoneNOBindingService;
    @Value("${binding.code.deadTime}")
    private String deadTime;

    /**
     * 手机绑定页面
     */
    @RequestMapping("/phoneNOBinding")
    public String phoneNOBinding(Model model) {
        //记录是否第一次打开index页面
        if (HttpUtils.getSession().getAttribute("phoneNOBindingFirstOpen") == null) {
            HttpUtils.getSession().setAttribute("phoneNOBindingFirstOpen", "NO");
        }
        model.addAttribute("deadTime", deadTime);
        return "/gtimp/phoneNOBinding";
    }

    /**
     * 获取验证码
     */
    @RequestMapping("/getPhoneValidCode")
    @ResponseBody
    public Result getPhoneValidCode(String phoneNO) {
        if (StringUtils.isEmpty(phoneNO) || !phoneNO.replaceAll("\\s", "").matches("^1[0-9]{10}$")) {
            return Result.newFailInstance("请检查输入的手机号码");
        }
        String code = phoneNOBindingService.getPhoneValidCode(phoneNO);
        return Result.newSuccessInstance(code);
    }

    /**
     * 用户手机绑定检查
     */
    @RequestMapping("/checkPhoneNOBinding")
    @ResponseBody
    public Result checkPhoneNOBinding() {
        if (phoneNOBindingService.checkPhoneNOBinding()) {
            return Result.newSuccessInstance("已经绑定");
        }
        return Result.newFailInstance("未绑定");
    }

    /**
     * 用户手机绑定
     */
    @RequestMapping("/bindPhone")
    @ResponseBody
    public Result bindPhone(String code, String phoneNO, String acceptMessage) {
        if (StringUtils.isEmpty(code)) {
            return Result.newFailInstance("请输入验证码");
        } else if (StringUtils.isEmpty(phoneNO) || !phoneNO.trim().matches("^1[0-9]{10}$")) {
            return Result.newFailInstance("请输入正确的手机号码");
        }
        String result = phoneNOBindingService.bindPhone(code, phoneNO, acceptMessage);
        if ("0".equals(result)) {//绑定成功
            return Result.newSuccessInstance("绑定成功");
        }
        return Result.newFailInstance(result);
    }

    /**
     * 解除绑定
     */
    @RequestMapping("/unbindPhoneNO")
    public String unbindPhoneNO() {
        phoneNOBindingService.unbindPhoneNO();
        return "/gtimp/phoneNOBinding";
    }

    /**
     * 接收信息更改
     */
    @RequestMapping("/acceptMsg")
    @ResponseBody
    public Result acceptMsg() {
        return Result.newSuccessInstance(phoneNOBindingService.acceptMsg());
    }

}
